package com.woniuxy.service.impl;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.woniuxy.config.SmsProperties;
import com.woniuxy.service.SendMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Set;

/**
 * 消息发送服务实现类
 * 实现消息发送相关的业务逻辑
 */
@Service("smsService")
@Slf4j
public class SendMsgServiceImpl implements SendMsgService {
    /**
     * 发送短信消息
     * @param mobile 手机号码
     * @param templateId 短信模板ID
     * @param params 模板参数数组
     */
    @Override
    public void sendMsg(String mobile, String templateId, String[] params) {
        //创建sdk客户端对象
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        //初始化客户端对象
        sdk.init(SmsProperties.SERVER_IP, SmsProperties.SERVER_PORT);
        //属性赋值
        sdk.setAccount(SmsProperties.ACCOUNT_ID, SmsProperties.ACCOUNT_TOKEN);
        sdk.setAppId(SmsProperties.APP_ID);
        sdk.setBodyType(BodyType.Type_JSON);
        String subAppend="1234";  //可选 扩展码，四位数字 0~9999
        Object resultCd = null;
        Object resultMSg = null;
        try {
            HashMap<String, Object> result = sdk.sendTemplateSMS(mobile, templateId, params);
            resultCd = result.get("statusCode");
            if("000000".equals(resultCd)){
                //正常返回输出data包体信息（map）
                HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
                Set<String> keySet = data.keySet();
                for(String key:keySet){
                    Object object = data.get(key);
                    System.out.println(key +" = "+object);
                }
            }else{
                //异常返回输出错误码和错误信息
                log.info("错误码>>" + resultCd +" 错误信息>> "+ resultMSg);
            }
        } catch(Exception e) {
            log.error("发送验证码异常");
        }
    }
}
